package com.cskaoyan.service;

import com.cskaoyan.bean.BaseReq;
import com.cskaoyan.bean.Comment;
import com.cskaoyan.bean.CommentExample;
import com.cskaoyan.bean.SystemExample.Criteria;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.data.CommentData;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: heliming
 * @date: 2021/6/4
 * @description:
 */
@Service
public class AdminCommentServiceImpl implements AdminCommentService {

    @Autowired
    CommentMapper commentMapper;

    @Override
    public CommentData selectCommentData(Integer page, Integer limit, String sort, String order, Integer valueId, Integer userId) {

        PageHelper.startPage(page, limit);
        CommentExample commentExample = new CommentExample();

        commentExample.setOrderByClause(sort + " " + order);
        CommentExample.Criteria criteria = commentExample.createCriteria();

        criteria.andDeletedEqualTo(false);
        if (valueId != null && !"".equals(valueId)) {
            criteria.andValueIdEqualTo(valueId);
        }
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }

        List<Comment> comments = commentMapper.selectByExample(commentExample);
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        long total = commentPageInfo.getTotal();

        CommentData commentData = new CommentData();
        commentData.setTotal(total);
        commentData.setItems(comments);


        return commentData;
    }

    @Override
    public void deleteComment(Comment comment) {

        Boolean deleted = true;
        comment.setDeleted(deleted);
        commentMapper.updateByPrimaryKeySelective(comment);


    }
}
